<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');

if (isset($_REQUEST['act']) && ($_REQUEST['act'] == 'query' ||  $_REQUEST['act'] == 'download'))
{
    if (strstr($_REQUEST['start_date'], '-') === false){
        $_REQUEST['start_date'] = local_date('Y-m-d', $_REQUEST['start_date']);
        $_REQUEST['end_date'] = local_date('Y-m-d', $_REQUEST['end_date']);
    }

    if ($_REQUEST['act'] == 'download'){
        $marketing_orderinfo = get_marketing_orderinfo(false);
        $filename = $_REQUEST['start_date'] . '_' . $_REQUEST['end_date'] . '绩效统计';

        header("Content-type: application/vnd.ms-excel; charset=utf-8");
        header("Content-Disposition: attachment; filename=$filename.csv");

        $data = "绩效统计\n";
        $data .= "排行,营销员,订单数(单位:个)\n";

        foreach ($marketing_orderinfo['marketing_orderinfo'] AS $k => $row){
            $order_by = $k + 1;
            $data .= "$order_by,$row[m_name],$row[order_num]\n";
        }
        echo ecs_iconv(EC_CHARSET, 'GB2312', $data);
        exit;
    }
    $marketing_orderinfo = get_marketing_orderinfo();
    $smarty->assign('filter',         $marketing_orderinfo['filter']);
    $smarty->assign('record_count',   $marketing_orderinfo['record_count']);
    $smarty->assign('page_count',     $marketing_orderinfo['page_count']);
    $smarty->assign('user_orderinfo', $marketing_orderinfo['marketing_orderinfo']);

    $sort_flag  = sort_flag($marketing_orderinfo['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('rg_report_mar.htm'), '', array('filter' => $marketing_orderinfo['filter'], 'page_count' => $marketing_orderinfo['page_count']));

}else{
    /* 时间参数 */
    if (!isset($_REQUEST['start_date'])) $start_date = local_strtotime(date('Y-m-01')); 
    if (!isset($_REQUEST['end_date'])) $end_date = local_strtotime(date('Y-m-d', strtotime(date('Y-m-01')." +1 month -1 day"))); 

    /* 取得会员排行数据 */
    $marketing_orderinfo = get_marketing_orderinfo();

    /* 赋值到模板 */
    $smarty->assign('ur_here',      '绩效统计');
    $smarty->assign('action_link',  array('text' => '下载绩效统计', 'href'=>"#download"));
    $smarty->assign('filter',           $marketing_orderinfo['filter']);
    $smarty->assign('record_count',     $marketing_orderinfo['record_count']);
    $smarty->assign('page_count',       $marketing_orderinfo['page_count']);
    $smarty->assign('user_orderinfo',	$marketing_orderinfo['marketing_orderinfo']);
    $smarty->assign('full_page',        1);
    $smarty->assign('start_date',     local_date('Y-m-d', $start_date));
    $smarty->assign('end_date',       local_date('Y-m-d', $end_date));
    $smarty->assign('sort_order_num',   '<img src="images/sort_desc.gif">');
    /* 页面显示 */
    assign_query_info();
    $smarty->display('rg_report_mar.htm');
}


/*------------------------------------------------------ */

function get_marketing_orderinfo($is_pagination = true){
    global $db, $ecs, $start_date, $end_date;

    $filter['start_date'] = empty($_REQUEST['start_date']) ? $start_date : local_strtotime($_REQUEST['start_date']);
    $filter['end_date'] = empty($_REQUEST['end_date']) ? $end_date : local_strtotime($_REQUEST['end_date']);
    $filter['sort_by'] = empty($_REQUEST['sort_by']) ? 'order_num' : trim($_REQUEST['sort_by']);
    $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
    
    $where = "WHERE m.m_id = o.m_id AND m.m_id > 0 " . order_query_sql('finished', 'o.');
    if ($filter['start_date']){
        $where .= " AND o.add_time >= '" . $filter['start_date'] . "'";
    }
    if ($filter['end_date']){
        $where .= " AND o.add_time <= '" . $filter['end_date'] . "'";
    }

    $sql = "SELECT count(distinct(m.m_id)) FROM " .$ecs->table('marketing')." AS m, ".$ecs->table('order_info')." AS o " .$where;
    $filter['record_count'] = $GLOBALS['db']->getOne($sql);
    /* 分页大小 */
    $filter = page_and_size($filter);
    
    $sql = "SELECT m.m_id, m.m_name, COUNT(*) AS order_num FROM ".$ecs->table('marketing')." AS m, ".$ecs->table('order_info')." AS o " .$where .
               " GROUP BY m.m_id"." ORDER BY " . $filter['sort_by'] . " " . $filter['sort_order'];
    if ($is_pagination){
        $sql .= " LIMIT " . $filter['start'] . ', ' . $filter['page_size'];
    }
    $marketing_orderinfo = array();
    $res = $db->query($sql);

    while ($items = $db->fetchRow($res)){
        $marketing_orderinfo[] = $items;
    }
    $arr = array('marketing_orderinfo' => $marketing_orderinfo, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
    return $arr;
}

?>